/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(function() {        
    $("#txtExpectDate").datepicker({
        dateFormat: 'dd-mm-yy',
        minDate: new Date()
    });
});

$(function() {        
    $("#txtReqDateStKey, #txtReqDateEnKey, #txtExpDateStKey, #txtExpDateEnKey").datepicker({
        dateFormat: 'dd-mm-yy'        
    });
});

function reqContact() {
        
    var dateNow = new Date();  
    
    var comId = $("#ddlConCom").val();
        
    //alert(ddlConComText);
    
    $.get('ApplicationReqContact',
        {comId: comId, dateNow: dateNow.getMilliseconds()},
        function(responseData) {       
            //alert("responseData = " + responseData);
            // Clear combobox
            var ddlConOption = document.getElementById("ddlCon");
            ddlConOption.options.length = 1;
            // Clear textbox
            $("#txtComAddress").val("");
            $("#txtConTel").val("");
            
            $(responseData).find('Messenger').each(function(){
                var comId = $(this).find('Company').find('Id').text();
                var comName = $(this).find('Company').find('Name').text();
                var comAddr = $(this).find('Company').find('Address').text();
                //alert('comId = '+comId+' comName = '+comName+' comAddr = '+comAddr);
                
                // Set data to company address
                $("#txtComAddress").val(comAddr);                
                
                var numRecCont = $(this).find('ContactSize').attr('NumRecData');
                //alert('numRecCont = '+numRecCont);

                if(numRecCont != 0) {
                    $(this).find('Contact').each(function(){
                        var conId = $(this).find('Id').text();
                        var conName = $(this).find('Name').text();
                        var conTel = $(this).find('Telephone').text();                        
                        //alert('conId = '+conId+' conName = '+conName+' conTel = '+conTel);
                        var op = new Option( conName , conId );
                        ddlConOption.options.add(op);
                    });
                }               
            });                        
        }
    );
}

function reqContactDetail() {
    
    var dateNow = new Date();  
    
    var conId = $("#ddlCon").val();
        
    //alert(conId);
    
    $.get("ApplicationReqContactDetail",
        {conId: conId, dateNow: dateNow.getMilliseconds()},
        function(responseData) {
            //alert(responseData);
            $(responseData).find('Messenger').each(function(){                
                var conId = $(this).find('Contact').find('Id').text();
                var conName = $(this).find('Contact').find('Name').text();
                var conTelephone = $(this).find('Contact').find('Telephone').text();
                //alert('conId = '+conId+' conName = '+conName+' conTelephone = '+conTelephone);
                $("#txtConTel").val(conTelephone);
            });
        }
    );
}

function addJob() {
        
    var boolValFlg = validateJob();
    // Found error
    if(boolValFlg == false) {
        return;
    }
        
    // Call ajax for insert job to database
    var dateNow = new Date();  
    var jobId = $("#hdJobId").val();
    var jobActionId = $("#ddlAction").val();
    var jobItemId = $("#ddlItem").val();
    var jobDesc = $("#txtDescription").text();
    //alert("jobId = " + jobId + " jobActionId = " + jobActionId + " jobItemId = " + jobItemId + " jobDesc = " + jobDesc);
        
    $.get("ApplicationAddJob",
        {jobId: jobId, jobActionId: jobActionId, jobItemId: jobItemId, jobDesc: jobDesc, dateNow: dateNow.getMilliseconds()},
        function(responseData) {
            //alert(responseData);
            
            //Set default
            $('#ddlAction').val('');
            $('#ddlItem').val('');                        
            $('#txtDescription').val('');  
                                                            
            $('#tblJob').empty();            
            $('#tblJob').append('<thead>');
            $('#tblJob').append('<td style="width: 10%; text-align: center;">' + 'ประเภท' + '</td>');
            $('#tblJob').append('<td style="width: 25%; text-align: center;">' + 'งาน' + '</td>');
            $('#tblJob').append('<td style="width: 45%; text-align: center;">' + 'รายละเอียด' + '</td>');
            $('#tblJob').append('<td style="width: 10%; text-align: center;">' + 'แก้ไข' + '</td>');
            $('#tblJob').append('<td style="width: 10%; text-align: center;">' + 'ลบ' + '</td>');
            $('#tblJob').append('</thead>');            
           
            $(responseData).find('Messenger').each(function(){                
                
                var numRecCont = $(this).find('JobDetailSize').attr('NumRecData');
                //alert("numRecCont = " + numRecCont);
                if(numRecCont != 0) {                    
                    $(this).find('JobDetail').each(function(){
                        var Id = $(this).find('Id').text();
                        var JobId = $(this).find('JobId').text();                        
                        var ActionId = $(this).find('ActionId').text();
                        var ActionName = $(this).find('ActionName').text();
                        var ItemId = $(this).find('ItemId').text();
                        var ItemName = $(this).find('ItemName').text();
                        var Description = $(this).find('Description').text();                
                        //alert("Id = " + Id + " JobId = " + JobId + " ActionId = " + ActionId + " ActionName = " + ActionName + " ItemId = " + ItemId + " ItemName = " + ItemName + " Description = " + Description);                                                                                                                                                
                        
                        //alert("tdEdit_" + Id);
                        var tdEdit = "tdEdit_" + Id;  
                        var tdDelete = "tdDelete_" + Id;
                        
                        $('#tblJob').append('<tbody>');
                        $('#tblJob').append('<td style="text-align: center;">' + ActionName + '</td>');
                        $('#tblJob').append('<td>' + ItemName + '</td>');
                        $('#tblJob').append('<td>' + Description + '</td>');
                        $('#tblJob').append('<td id="' + tdEdit +'" style="text-align: center;"><img src="../images/edit.png"></td>');
                        $('#tblJob').append('<td id="' + tdDelete +'" style="text-align: center;"><img src="../images/delete.png"></td>');
                        $('#tblJob').append('</tbody>');
                        
                        // Add attribute to td
                        $("#" + tdEdit).attr("onclick", "editJobDetail(" + Id + ", " + ActionId + ", " + ItemId + ", '" + Description + "')");
                        $("#" + tdDelete).attr("onclick", "deleteJob(" + Id + ", '" + ItemName + "')");
                        
                    });                     
                }                                 
            });             
        }
    );        
}

function validateJob() {
    
    var boolValFlg = true;
    
    var ddlAction = $("#ddlAction").val();
    var ddlItem = $("#ddlItem").val();
    
    var ErrMess = "กรุณาตรวจสอบค่าต่อไปนี้ : ";
    
    if(ddlAction == "") {
        ErrMess += "\n-: ประเภท";  
        boolValFlg = false;
    }
    if(ddlItem == "") {
        ErrMess += "\n-: งาน";
        boolValFlg = false;
    }
    
    if(boolValFlg == false) {
        alert(ErrMess);
    }   
    
    return boolValFlg;
}

function editJobDetail(jobDetailId, actionId, itemId, desc) {        
    
    //alert("jobDetailId = " + jobDetailId + " actionId = " + actionId + " itemId = " + itemId + " desc = " + desc);
    // Set button
    $('#btnAddJobDetail').attr('disabled', 'disabled');
    $('#btnEdtiJobDetail').removeAttr('disabled');
    $('#btnCancelJobDetail').removeAttr('disabled');
    
    desc = convertStr("<br/>", "\n", desc);
    
    // Set data to job detail
    $('#ddlAction').val(actionId);
    $('#ddlItem').val(itemId);        
    $('#txtDescription').val(desc);
    
    // Set data to hidden jobDetailId
    $('#hdJobDetailId').val(jobDetailId);
}

function deleteJob(jobDetailId, itemName) {
    //alert("deleteJobDetail = " + jobDetailId + " itemName = " + itemName);
    var result = confirm("คุณต้องการลบข้อมูล ? : " + itemName);
    if (result == false) {
        return;
    } 
    
    var dateNow = new Date();  
    var jobId = $("#hdJobId").val();    
    //alert("jobId = " + jobId + " jobDetailId = " + jobDetailId);
    
    $.get("ApplicationDeleteJob",
        {jobId: jobId, jobDetailId: jobDetailId, dateNow: dateNow.getMilliseconds()},
        function(responseData) {
            //alert(responseData);
            
            //Set default
            $('#ddlAction').val('');
            $('#ddlItem').val('');                        
            $('#txtDescription').val('');  
            
            // Set button
            $('#btnAddJobDetail').removeAttr('disabled');
            $('#btnEdtiJobDetail').attr('disabled', 'disabled');
            $('#btnCancelJobDetail').attr('disabled', 'disabled');
                                                            
            $('#tblJob').empty();            
            $('#tblJob').append('<thead>');
            $('#tblJob').append('<td style="width: 10%; text-align: center;">' + 'ประเภท' + '</td>');
            $('#tblJob').append('<td style="width: 25%; text-align: center;">' + 'งาน' + '</td>');
            $('#tblJob').append('<td style="width: 45%; text-align: center;">' + 'รายละเอียด' + '</td>');
            $('#tblJob').append('<td style="width: 10%; text-align: center;">' + 'แก้ไข' + '</td>');
            $('#tblJob').append('<td style="width: 10%; text-align: center;">' + 'ลบ' + '</td>');
            $('#tblJob').append('</thead>');            
           
            $(responseData).find('Messenger').each(function(){                
                
                var numRecCont = $(this).find('JobDetailSize').attr('NumRecData');
                //alert("numRecCont = " + numRecCont);
                if(numRecCont != 0) {                    
                    $(this).find('JobDetail').each(function(){
                        var Id = $(this).find('Id').text();
                        var JobId = $(this).find('JobId').text();                        
                        var ActionId = $(this).find('ActionId').text();
                        var ActionName = $(this).find('ActionName').text();
                        var ItemId = $(this).find('ItemId').text();
                        var ItemName = $(this).find('ItemName').text();
                        var Description = $(this).find('Description').text();                
                        //alert("Id = " + Id + " JobId = " + JobId + " ActionId = " + ActionId + " ActionName = " + ActionName + " ItemId = " + ItemId + " ItemName = " + ItemName + " Description = " + Description);                                                                                                                                                
                        
                        //alert("tdEdit_" + Id);
                        var tdEdit = "tdEdit_" + Id;  
                        var tdDelete = "tdDelete_" + Id;
                        
                        $('#tblJob').append('<tbody>');
                        $('#tblJob').append('<td style="text-align: center;">' + ActionName + '</td>');
                        $('#tblJob').append('<td>' + ItemName + '</td>');
                        $('#tblJob').append('<td>' + Description + '</td>');
                        $('#tblJob').append('<td id="' + tdEdit +'" style="text-align: center;"><img src="../images/edit.png"></td>');
                        $('#tblJob').append('<td id="' + tdDelete +'" style="text-align: center;"><img src="../images/delete.png"></td>');
                        $('#tblJob').append('</tbody>');
                        
                        // Add attribute to td                        
                        $("#" + tdEdit).attr("onclick", "editJobDetail(" + Id + ", " + ActionId + ", " + ItemId + ", '" + Description + "')");
                        $("#" + tdDelete).attr("onclick", "deleteJob(" + Id + ", '" + ItemName + "')");
                        
                    });                     
                }                                 
            });             
        }
    );        
}

function cancelEdit() {
    
    // Set default    
    $('#ddlAction').val('');
    $('#ddlItem').val('');                        
    $('#txtDescription').val(''); 
    
    // Set button
    $('#btnAddJobDetail').removeAttr('disabled');
    $('#btnEdtiJobDetail').attr('disabled', 'disabled');
    $('#btnCancelJobDetail').attr('disabled', 'disabled');
}

function editJob() {
    //alert($('#hdJobDetailId').val())
    
    var boolValFlg = validateJob();
    // Found error
    if(boolValFlg == false) {
        return;
    }
        
    // Call ajax for insert job to database
    var dateNow = new Date();  
    var jobId = $("#hdJobId").val();
    var jobActionId = $("#ddlAction").val();
    var jobItemId = $("#ddlItem").val();
    var jobDesc = $("#txtDescription").text();
    var jobDetailId = $('#hdJobDetailId').val();
    //alert("jobActionId = " + jobActionId + " jobItemId = " + jobItemId + " jobDesc = " + jobDesc + " jobDetailId = " + jobDetailId);
        
    $.get("ApplicationEditJob",
        {jobId: jobId, jobDetailId: jobDetailId, jobActionId: jobActionId, jobItemId: jobItemId, jobDesc: jobDesc, dateNow: dateNow.getMilliseconds()},
        function(responseData) {
            //alert(responseData);
            
            //Set default
            $('#ddlAction').val('');
            $('#ddlItem').val('');                        
            $('#txtDescription').val('');  
            
            // Set button
            $('#btnAddJobDetail').removeAttr('disabled');
            $('#btnEdtiJobDetail').attr('disabled', 'disabled');
            $('#btnCancelJobDetail').attr('disabled', 'disabled');
                                                            
            $('#tblJob').empty();            
            $('#tblJob').append('<thead>');
            $('#tblJob').append('<td style="width: 10%; text-align: center;">' + 'ประเภท' + '</td>');
            $('#tblJob').append('<td style="width: 25%; text-align: center;">' + 'งาน' + '</td>');
            $('#tblJob').append('<td style="width: 45%; text-align: center;">' + 'รายละเอียด' + '</td>');
            $('#tblJob').append('<td style="width: 10%; text-align: center;">' + 'แก้ไข' + '</td>');
            $('#tblJob').append('<td style="width: 10%; text-align: center;">' + 'ลบ' + '</td>');
            $('#tblJob').append('</thead>');            
           
            $(responseData).find('Messenger').each(function(){                
                
                var numRecCont = $(this).find('JobDetailSize').attr('NumRecData');
                //alert("numRecCont = " + numRecCont);
                if(numRecCont != 0) {                    
                    $(this).find('JobDetail').each(function(){
                        var Id = $(this).find('Id').text();
                        var JobId = $(this).find('JobId').text();                        
                        var ActionId = $(this).find('ActionId').text();
                        var ActionName = $(this).find('ActionName').text();
                        var ItemId = $(this).find('ItemId').text();
                        var ItemName = $(this).find('ItemName').text();
                        var Description = $(this).find('Description').text();                
                        //alert("Id = " + Id + " JobId = " + JobId + " ActionId = " + ActionId + " ActionName = " + ActionName + " ItemId = " + ItemId + " ItemName = " + ItemName + " Description = " + Description);                                                                                                                                                
                        
                        //alert("tdEdit_" + Id);
                        var tdEdit = "tdEdit_" + Id;  
                        var tdDelete = "tdDelete_" + Id;
                        
                        $('#tblJob').append('<tbody>');
                        $('#tblJob').append('<td style="text-align: center;">' + ActionName + '</td>');
                        $('#tblJob').append('<td>' + ItemName + '</td>');
                        $('#tblJob').append('<td>' + Description + '</td>');
                        $('#tblJob').append('<td id="' + tdEdit +'" style="text-align: center;"><img src="../images/edit.png"></td>');
                        $('#tblJob').append('<td id="' + tdDelete +'" style="text-align: center;"><img src="../images/delete.png"></td>');
                        $('#tblJob').append('</tbody>');
                        
                        // Add attribute to td                        
                        $("#" + tdEdit).attr("onclick", "editJobDetail(" + Id + ", " + ActionId + ", " + ItemId + ", '" + Description + "')");
                        $("#" + tdDelete).attr("onclick", "deleteJob(" + Id + ", '" + ItemName + "')");
                        
                    });                     
                }                                 
            });             
        }
    );        
}

function confirmDelete(jobId) {       
        
    $.get('../GetMessage',
        {mesId:"001"},
        function(responseData) {       
            //alert(responseData);
            $(responseData).find('Messenger').each(function(){                    
                var MesId = $(this).find('Message').find('MesId').text();
                var MesType = $(this).find('Message').find('MesType').text();
                var MesDetail = $(this).find('Message').find('MesDetail').text();
                //alert('MesId = '+MesId+' MesType = '+MesType+' MesDetail = '+MesDetail);                                       

                var result = confirm(MesDetail + ": " + jobId);
                if (result == true) {
                    location.href = "ApplicationDelete?jobId="+jobId;
                }                     
            });
        }
    );                
}

function msgApprove(jobId, ddlEmployId, adminFlag, express) {
    
    var ddlEmployIdVal = $("#" + ddlEmployId).val();  
    var txtMsgRemark = $("#txtMsgRemark").text();
    
    txtMsgRemark = convertStr("\n", "<br/>", txtMsgRemark);
    
    //alert("jobId = " + jobId + " ddlEmployIdVal = " + ddlEmployIdVal + " adminFlag = " + adminFlag + " txtMsgRemark = " + txtMsgRemark + " express = " + express);
    if(adminFlag == "true" && ddlEmployIdVal == "") {
        $.get('../GetMessage',
            {mesId:"030"},
            function(responseData) {       
                //alert(responseData);
                $(responseData).find('Messenger').each(function(){                    
                    var MesId = $(this).find('Message').find('MesId').text();
                    var MesType = $(this).find('Message').find('MesType').text();
                    var MesDetail = $(this).find('Message').find('MesDetail').text();
                    //alert('MesId = '+MesId+' MesType = '+MesType+' MesDetail = '+MesDetail);                                       

                    alert(MesDetail);
                });
            }
        );     
    } else {        
        location.href="ApplicationMsgApprove?appId=" + jobId + "&ddlEmploy=" + ddlEmployIdVal + "&txtMsgRemark=" + txtMsgRemark + "&express=" + express;
    }            
}

function msgReject() {
    
    var txtMsgRemark = $("#txtMsgRemark").text();
    var jobId = $("#hdJobId").val();
    
    txtMsgRemark = convertStr("\n", "<br/>", txtMsgRemark);
    
    if(txtMsgRemark == "") {        
        $.get('../GetMessage',
            {mesId:"031"},
            function(responseData) {       
                //alert(responseData);
                $(responseData).find('Messenger').each(function(){                    
                    var MesId = $(this).find('Message').find('MesId').text();
                    var MesType = $(this).find('Message').find('MesType').text();
                    var MesDetail = $(this).find('Message').find('MesDetail').text();
                    //alert('MesId = '+MesId+' MesType = '+MesType+' MesDetail = '+MesDetail);                                       

                    alert(MesDetail);
                });
            }
        );                 
    } else {        
        location.href="ApplicationMsgReject?appId=" + jobId + "&txtMsgRemark=" + txtMsgRemark;
    }
}

function msgComplete() {
    
    var txtMsgRemark = $("#txtMsgRemark").text();
    var jobId = $("#hdJobId").val();
    
    txtMsgRemark = convertStr("\n", "<br/>", txtMsgRemark);
    
    location.href="ApplicationMsgComplete?appId=" + jobId + "&txtMsgRemark=" + txtMsgRemark;
}

function msgNotComplete() {
    
    var txtMsgRemark = $("#txtMsgRemark").text();
    var jobId = $("#hdJobId").val();
    
    txtMsgRemark = convertStr("\n", "<br/>", txtMsgRemark);
    
    if(txtMsgRemark == "") {        
        $.get('../GetMessage',
            {mesId:"031"},
            function(responseData) {       
                //alert(responseData);
                $(responseData).find('Messenger').each(function(){                    
                    var MesId = $(this).find('Message').find('MesId').text();
                    var MesType = $(this).find('Message').find('MesType').text();
                    var MesDetail = $(this).find('Message').find('MesDetail').text();
                    //alert('MesId = '+MesId+' MesType = '+MesType+' MesDetail = '+MesDetail);                                       

                    alert(MesDetail);
                });
            }
        );                 
    } else {        
        location.href="ApplicationMsgNotComplete?appId=" + jobId + "&txtMsgRemark=" + txtMsgRemark;
    }
}

function convertStr(regFrom, regTo, strValue) {
                
    do {
        var searchBr = -1;
        searchBr = strValue.search(regFrom);
        strValue = strValue.replace(regFrom, regTo);
    } while (searchBr != -1);
    
    return strValue;
}
